package com.atguigui.leetcode1;

/**
 * 6909. 最长奇偶子数组
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/7/3 13:15
 */
public class P6909LongestEvenOddSubarrayWithThreshold {

    public static void main(String[] args) {
        Solution solution = new P6909LongestEvenOddSubarrayWithThreshold().new Solution();
        int[] arr = {2, 3, 4, 5};
        solution.longestAlternatingSubarray(arr, 4);
    }

    class Solution {
        public int longestAlternatingSubarray(int[] nums, int threshold) {
            int ans = 0;
            int l = 0, r = 0;
            int n = nums.length;
            for (int i = 0; i < n; i++) {
                if (nums[i] % 2 != 0 || nums[i] > threshold) {
                    continue;
                }
                l = i;
                r = i;
                int f = nums[i] % 2;
                for (int j = i + 1; j < n; j++) {
                    if (nums[j] % 2 == f || nums[j] > threshold) {
                        break;
                    }
                    r = j;
                    f = nums[j] % 2;
                }
                ans = Math.max(ans, r - l + 1);
            }

            return ans;
        }
    }
}
